﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Data;
using DiemChapNhanThe.Entities;
using System.Data;

public partial class Admin_LienHe_ThongTinLienHe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        try
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
                //tim kiem theo dia chi va noi dung
                WhereClause += " AND DiaChi like N'%" + Common.StandardString(txtDiaChi.Text) + "%' or NoiDung like N'%"+Common.StandardString(txtNoiDung.Text)+"%'";
            DataSet dsLienHe = DataRepository.Provider.ExecuteDataSet("LienHe_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsLienHe.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsLienHe.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }
        catch (Exception)
        {
        }
    }

    /// <summary>
    /// Phân trang Grid view
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLLienHe.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Bind_Data(Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("Admin/LienHe/QLLienHe.aspx.cs", "cmdTimKiem_Click", ex.ToString());
        }
    }

    protected void cmdTimLai_Click(object sender, ImageClickEventArgs e)
    {
        txtDiaChi.Text = string.Empty;
        txtNoiDung.Text = string.Empty;
    }
    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

                HiddenField H_IdCatalog = (HiddenField)e.Row.FindControl("H_IdCatalog");
                e.Row.Attributes.Add("ondblclick", "Javascript:DoubleClick_LienHe('" + H_IdCatalog.Value + "');");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("Admin/LienHe/QLLienHe.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
        }
    }
    protected void cmdXoa_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gridCatalog.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                HiddenField H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                bool deleteStatus = DataRepository.LienHeProvider.Delete(Convert.ToInt32(H_IdCatalog.Value));

            }
        }
        Bind_Data(Pager1.CurrentIndex);
    }
}